package org.lalib.mediator;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.hibernate.criterion.Restrictions;
import org.lalib.acquire.IMediator;
import org.lalib.entity.object.EHistoricalValue;
import org.lalib.entity.object.EItem;
import org.lalib.entity.object.variable.EVariable;
import org.lalib.foundation.FDataAccess;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@SuppressWarnings("rawtypes")
@Repository("mHistoricalValue")
public class MHistoricalValue extends FDataAccess<EHistoricalValue> implements IMediator {

	@Autowired
	private MVariable mVariable;

	public void setmVariable(MVariable mVariable) {
		this.mVariable = mVariable;
	}

	private static final long serialVersionUID = 1L;

	@Override
	protected Class<EHistoricalValue> getClassType() {
		return EHistoricalValue.class;
	}

	public EHistoricalValue getHistoricalValue(EItem item, EVariable variable, Date since) {
		return ((EHistoricalValue) getSession().createCriteria(getClassType()).add(Restrictions.eq("item", item))
				.add(Restrictions.eq("variable", variable)).add(Restrictions.eq("since", since)).uniqueResult());
	}

	@SuppressWarnings("unchecked")
	public List<EHistoricalValue> getAllHistoricalValues(EItem item) {
		return ((List<EHistoricalValue>) getSession().createCriteria(getClassType()).add(Restrictions.eq("item", item))
				.add(Restrictions.isNull("till")).list());
	}

	public EHistoricalValue getValueByVariableName(EItem item, String variableName) {
		EVariable variable = mVariable.getVariableByName(variableName);
		return getNewestValue(item, variable);
	}

	public EHistoricalValue getNewestValue(EItem item, EVariable variable) {
		return ((EHistoricalValue) getSession().createCriteria(getClassType()).add(Restrictions.eq("variable", variable))
				.add(Restrictions.or(Restrictions.isNull("till"), Restrictions.ge("till", new Date()))).add(Restrictions.eq("item", item))
				.uniqueResult());
	}

	@SuppressWarnings("unchecked")
	public List<EHistoricalValue> getAllHistoricalValues(EItem item, EVariable variable) {
		return getSession().createSQLQuery("SELECT * FROM `historicalvalue` WHERE item =" + item.getId() + "  and variable=" + variable.getId())
				.addEntity(getClassType()).list();
	}

	@SuppressWarnings("unchecked")
	public List<EHistoricalValue> getValues(EVariable variable, String value) {
		List<EHistoricalValue> historicalValues = getSession()
				.createSQLQuery("SELECT * FROM historicalvalue WHERE variable = " + variable.getId() + " AND stringcore like '" + value + "'")
				.addEntity(getClassType()).list();
		if (historicalValues == null)
			historicalValues = new ArrayList<>();
		return historicalValues;
	}

	public EHistoricalValue getItemFromStringValue(EVariable variable, String object) {
		return (EHistoricalValue) getSession()
				.createSQLQuery("SELECT * FROM historicalvalue where stringcore like '" + object + "' AND variable = " + variable.getId())
				.addEntity(getClassType()).uniqueResult();
	}
}